package com.example.j280_webproject.day037;

import java.sql.*;

public class JDBC_Register {
    public boolean register(String username,String password,int age) throws ClassNotFoundException, SQLException {
        boolean action = false;
        String driver = "com.mysql.jdbc.Driver";    //固定写法
        Class.forName(driver);                      //通过反射机制 把指定的类加载到当前类中

        //warning
        String url = "jdbc:mysql://localhost:3306/mygxa?useSSL=false&characterEncoding=UTF-8";
        String db_user = "root";
        String db_password = "root";
        Connection conn = DriverManager.getConnection(url, db_user, db_password);
        System.out.println("连接成功");
        String sql="insert into user(username, password,age) values (?,?,?)";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setString(1,username);
        preparedStatement.setString(2,password);
        preparedStatement.setInt(3,age);
        preparedStatement.executeUpdate();

        conn.close();
        action = true;
        return action;
    }
}
